package com.cxrj.zhsq.propertyManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.propertyManage.dao.ReleaseMapper;
import com.cxrj.zhsq.propertyManage.dao.WorkOrderMapper;
import com.cxrj.zhsq.propertyManage.entity.Release;
import com.cxrj.zhsq.propertyManage.entity.WorkOrder;
import com.cxrj.zhsq.propertyManage.service.WorkOrderService;

@Service
public class WorkOrderServiceImpl implements WorkOrderService {
	
	@Resource
	private WorkOrderMapper workOrderMapper;
	@Resource
	private ReleaseMapper releaseMapper;

	@Override
	public Map<String, Object> insertAndUpdateWorkOrder(WorkOrder workOrder) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			if ("".equals(workOrder.getWorkOrderId())) {
				workOrder.setWorkOrderId(UUID.randomUUID().toString());
				workOrder.setWorkOrderTime(new Date());
				workOrder.setWorkOrderState("1");
				workOrderMapper.insertSelective(workOrder);
				
			}else{
				workOrderMapper.updateByPrimaryKeySelective(workOrder);
			}
			resultMap.put("result", "1");
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		
		return resultMap;
	}
	
	@Override
	public Map<String, Object> insertWorkOrder(WorkOrder workOrder) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			Release release=new Release();
			release.setAppealId(workOrder.getWorkOrderReleaseid());
			release.setS1("1");
			releaseMapper.updateByPrimaryKeySelective(release);
			workOrder.setWorkOrderTime(new Date());
			workOrder.setWorkOrderState("1");
			workOrderMapper.insertSelective(workOrder);
			resultMap.put("result", "1");
				
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		
		return resultMap;
	}

	@Override
	public Map<String, Object> selectOrderPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String, Object> sqlMap=new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		//数据起始位置
		String start=request.getParameter("offset");
		//数据长度
		String length=request.getParameter("limit");
		//总条数
		String recordsTotal = "0";
		//查询条件
		String kod=request.getParameter("kod");
		String kodType=request.getParameter("kodType");
		String sqlStr = " where 1=1 ";
		
		if(kodType != null && !kodType.equals("")){
			sqlStr += "and work_order_state = " + kodType;
		}
		
		if(kod != null && !kod.equals("")){
			sqlStr += " and ( work_order_title like " + '"' + '%' + kod + '%' + '"'+" or work_order_people like " + '"' + '%' + kod + '%' + '"'+')';
		}
		
		
		sqlMap.put("condition",sqlStr);
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		recordsTotal=workOrderMapper.getOrderCount(sqlStr)+"";
		List<Map<String, Object>> workOrderList=workOrderMapper.selectOrderPage(sqlMap);
		
		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", workOrderList);
		
		return resultMap;
	}

	@Override
	public Map<String, Object> selectById(String workOrderId) {
		Map<String, Object> resultMap =new HashMap<>();
		try {
			resultMap.put("workOrder", workOrderMapper.selectByPrimaryKey(workOrderId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateWorkOrder(WorkOrder workOrder) {
		Map<String, Object> resultMap=new HashMap<>();
		int a=workOrderMapper.updateByPrimaryKeySelective(workOrder);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteWorkOrder(String workOrderId) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			//UploadController.deleteDir(folderName, request, response)
			workOrderMapper.deleteByPrimaryKey(workOrderId);
			resultMap.put("result", "1");
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		return resultMap;
	}

	

}
